package com.td.module.equipment.framework.core;

import cn.hutool.http.HttpUtil;
import com.td.framework.common.util.json.JsonUtils;
import com.td.module.equipment.controller.admin.info.vo.HistoryReqVO;
import com.td.module.equipment.framework.config.RkProperties;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Objects;
import java.util.concurrent.TimeUnit;

@Slf4j
@Component
public class RkClient {

    @Resource
    private RkProperties rkProperties;

    @Resource
    private StringRedisTemplate stringRedisTemplate;

    private HashMap<String, Object> request(HashMap<String, Object> params, String uri, String token) {
        String responseText = null;
        try {
            if (token == null) {
                responseText = HttpUtil.get(rkProperties.getUrl() + uri, params);
            } else {
                HashMap<String, String> headers = new HashMap<>();
                headers.put("authorization", token);
                responseText = HttpUtil.createGet(rkProperties.getUrl() + uri).addHeaders(headers).form(params).execute().body();
            }
            HashMap<String, Object> responseObj = JsonUtils.parseObject(responseText, HashMap.class);
            if (Objects.equals(1000, responseObj.get("code"))) {
                return responseObj;
            } else {
                log.info("[仁科接口异常]", responseObj);
                return null;
            }

        } catch (Exception e) {
            log.info("[仁科连接异常]", e);
            return null;
        }

    }

    /**
     * 设备列表接口
     *
     * @return
     * @throws Throwable
     */
    public HashMap<String, Object> login() {

        String cacheKey = "rk.login";
        HashMap<String, Object> loginData = JsonUtils.parseObject(stringRedisTemplate.opsForValue().get(cacheKey), HashMap.class);
        if (loginData == null) {

            HashMap<String, Object> params = new HashMap<>();
            params.put("loginName", rkProperties.getUsername());
            params.put("password", rkProperties.getPassword());

            HashMap<String, Object> loginObject = request(params, "/getToken", null);
            loginData = (HashMap<String, Object>) loginObject.get("data");
            if (Objects.equals(1000, loginObject.get("code"))) {
                stringRedisTemplate.opsForValue().set(cacheKey, JsonUtils.toJsonString(loginData), 7000L, TimeUnit.SECONDS);
            }

        }

        return loginData;

    }

    /**
     * 获取仁科设备列表
     *
     * @return
     */
    public List<HashMap<String, Object>> deviceList() {
        HashMap<String, Object> loginData = login();
        if (loginData == null) {
            return null;
        }

        HashMap<String, Object> params = new HashMap<>();
        return (List<HashMap<String, Object>>) request(params, "/device/getDeviceList",
                String.valueOf(loginData.get("token"))).get("data");
    }

    /**
     * 获取仁科历史数据
     */
    public List<HashMap<String, Object>> realTimeList() {
        HashMap<String, Object> loginData = login();
        if (loginData == null) {
            return null;
        }
        HashMap<String, Object> params = new HashMap<>();

        return (List<HashMap<String, Object>>) request(params, "/data/getRealTimeData",
                String.valueOf(loginData.get("token"))).get("data");
    }

    /**
     * 获取仁科历史数据
     */
    public List<HashMap<String, Object>> historyList(HistoryReqVO reqVO) {
        HashMap<String, Object> loginData = login();
        if (loginData == null) {
            return null;
        }
        HashMap<String, Object> params = new HashMap<>();

         params.put("deviceAddr",reqVO.getDeviceAddr());
         params.put("nodeId",reqVO.getNodeId());
         params.put("startTime",reqVO.getStartTime());
         params.put("endTime",reqVO.getEndTime());

        return (List<HashMap<String, Object>>) request(params, "/data/historyList",
                String.valueOf(loginData.get("token"))).get("data");
    }
}
